import { BaseUrl } from "@/api";
import { ResultData } from "@/api/interface";
import { RespOk } from "@/constant/codes";
import { ElMessage } from "element-plus";
import { ReqGetDicOptions } from "@/api/interface/SysDictionary/ReqGetDicOptions";
import { DicOption } from "@/api/interface/SysDictionary/DicOption";
import { useUserStore } from "@/stores/modules/user";
import { ApiSysDictionaryGetOptions } from "@/api/apis/sys_dictionary";
import { formCreateFetch } from "@/components/FormCreate/interface/fetch";

export const getDicOptions = (dicKeyName: string, method = "POST"): formCreateFetch | null => {
  const userStore = useUserStore();
  if (dicKeyName == "") {
    return null;
  }
  //fetch:
  return {
    action: BaseUrl + ApiSysDictionaryGetOptions,
    to: "options",
    method: "POST",
    data: { dic_key_name: dicKeyName } as ReqGetDicOptions,
    //调用接口附带数据的提交方式,默认为 `formData`
    dataType: "json",
    //请求头 带上鉴权信息
    headers: { Authorization: "Bearer " + userStore.token },
    parse(res: ResultData<DicOption[]>) {
      if (res.code == RespOk) {
        return res.data;
      } else {
        ElMessage({ type: "error", message: "加载失败数据失败" + res.msg });
      }
    }
  };
};
